<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // typeof

      console.log(typeof undefined); // "undefined"
      console.log(typeof true); // "boolean"
      console.log(typeof 42); // "number"
      console.log(typeof "Hello"); // "string"
      console.log(typeof Symbol()); // "symbol"
      console.log(typeof 123n); // "bigint"

      // instanceof
      const arr = [1, 2, 3];
      console.log(arr instanceof Array); // true
      console.log(arr instanceof Object); // true (因为 Array 继承自 Object)

      const date = new Date();
      console.log(date instanceof Date); // true
      console.log(date instanceof Object); // true

      function Person(name) {
        this.name = name;
      }
      const john = new Person("John");
      console.log(john instanceof Person); // true
      console.log(john instanceof Object); // true
      // constructor

      const arr1 = [1, 2, 3];
      console.log(arr1.constructor === Array); // true

      const obj = {};
      console.log(obj.constructor === Object); // true

      const num = 123;
      console.log(num.constructor === Number); // true

      const str = "hello";
      console.log(str.constructor === String); // true

      console.log(Object.prototype.toString.call(undefined)); // [object Undefined]
      console.log(Object.prototype.toString.call(null)); // [object Null]
      console.log(Object.prototype.toString.call(true)); // [object Boolean]
      console.log(Object.prototype.toString.call(123)); // [object Number]
      console.log(Object.prototype.toString.call("hello")); // [object String]
      console.log(Object.prototype.toString.call(Symbol())); // [object Symbol]
      console.log(Object.prototype.toString.call(10n)); // [object BigInt]
      function getType(obj) {
        return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
      }

      console.log(getType([])); // 'array'
      console.log(getType(null)); // 'null'
      console.log(getType(new Date())); // 'date'
      console.log(getType(/regex/)); // 'regexp'
    </script>
  </body>
</html>
